
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  client;

 import java.awt.*; 
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
  

import javax.swing.*;




 
public class RequestGUI extends JFrame implements ActionListener
 { 
	
	private static final long serialVersionUID = 1L;
	// Variables declaration
 	private JLabel jLabel1; 
 	private  JLabel jLabel;
 	private JLabel labelServerIp; 
 	private JTextField Username;
	private JTextField Message;
	private JTextField textServerIp;
 	private JButton Send;
 	private JButton Exit;
 	private JPanel contentPane;
 	// End of variables declaration
 	
 	//Connection Data
 	BufferedReader inputPort;
	PrintStream outputport;
	String inputData;
	
 	public RequestGUI() 
 	{ 
 		super(); 
 		initializeComponent();
 		this.setVisible(true); 
 		
 	} 
 	private void initializeComponent() 
 	{ 
 		jLabel  = new JLabel("UserName");
 		jLabel1 = new JLabel(); 
 		labelServerIp = new JLabel("Server IP");
 		Message = new JTextField(); 
 		Username = new JTextField();
 		textServerIp = new JTextField("localhost");
 		Send = new JButton();
 		Exit = new JButton(); 
 		contentPane = (JPanel)this.getContentPane(); 
  
 		jLabel1.setText("Enter Request"); 
 		Send.setText("Send"); 
 		Exit.setText("Exit"); 
 		contentPane.setLayout(null);
 		
 		Username.setToolTipText("Enter your Mobile number");
 		Message.setToolTipText("Enter your request");
 		
 		addComponent(contentPane, labelServerIp, 15,20,85,35);
 		addComponent(contentPane, textServerIp, 115,22,296,33);
 		
 		addComponent(contentPane, jLabel, 15,52,85,35); 
 		addComponent(contentPane, Username, 115,57,296,33);
 		
 		addComponent(contentPane, jLabel1, 15,90,85,35); 
 		addComponent(contentPane, Message, 115,91,296,33);
 		
 		addComponent(contentPane, Send, 140,130,80,30); 
 		addComponent(contentPane, Exit, 220,130,80,30);
 		
 		Send.addActionListener(this);
 		Exit.addActionListener(this);
 		
 		
 		this.setTitle("Request GUI"); 
 		this.setLocation(new Point(0, 0)); 
 		this.setSize(new Dimension(457, 220)); 		
 	} 
  
 	
 	private void addComponent(Container container,Component c,int x,int y,int width,int height) 
 	{ 
 		
 		c.setBounds(x,y,width,height); 
 		container.add(c); 
 		
 	} 
 	
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==Send)
		{
			try
			{
				  	//Socket socket = new Socket("192.168.0.236",9999);
				
					String ip = textServerIp.getText().trim();
					if(ip.length()==0)
					ip = "localhost";
					Socket socket = new Socket(ip,9999);
				
					outputport = new PrintStream(socket.getOutputStream());
					outputport.println(Username.getText());
					outputport.flush();
					outputport.println(Message.getText());
					outputport.flush();
					
					inputPort = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					
					String input = inputPort.readLine();
					
					StringBuffer temp=new StringBuffer();
					int i =0;
					while(true)
					{
						if(input.contains("&"))
						{
							System.out.println("input : "+input); 
							i= input.indexOf("&");
							String first = input.substring(0, i);
							input = input.substring(i+1); 
							temp.append(first);
							temp.append("\n");
						}
						else
						{
							temp.append(input);
							break;
						}
					}
					
					JOptionPane.showMessageDialog(this, temp);
					System.out.println(input);
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(this, "Unable to connect Server");
				System.out.println("Unable to connect Server");
				//e.printStackTrace();
			}
		}
		if(ae.getSource()==Exit)
		{
			this.dispose();
 	 		System.exit(0);
		}
	}
 	public static void main(String[] args) 
 	{ 
 		new RequestGUI();
 	}
	
}